package com.jdrx.sw.sewage.service;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.jdrx.sw.sewage.common.enums.EAiStatisticsType;
import com.jdrx.sw.sewage.common.util.LocalDateTimeUtils;
import com.jdrx.sw.sewage.dao.AiModelParamDAO;
import com.jdrx.sw.sewage.dao.AiReportDAO;
import com.jdrx.sw.sewage.service.base.BaseServiceImpl;
import com.jdrx.sw.sewage.service.common.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

import static com.jdrx.sw.sewage.common.constant.Constant.*;

@Service
public class AiModelParamServiceImpl extends BaseServiceImpl {

    public static final Logger logger = LoggerFactory.getLogger(AiModelServiceImpl.class);

    @Autowired
    private AiModelParamDAO aiModelParamDAO;
    @Autowired
    private RedisService redisService;
    @Autowired
    private AiReportDAO aiReportDAO;

    public Map<String, Object> findStatistics(String parentName) {
        Map<String, Object> map = Maps.newHashMap();

        Map<String, Object> gather = Maps.newHashMap();
        String gatherNum = redisService.getStr(CARBON_SOURCE_ADD_GATHER_NUM);
        gather.put("num", gatherNum);
        gather.put("data", aiModelParamDAO.findPointCrrVal(parentName));

        Map<String, Object> clean = Maps.newHashMap();
        Long cleanNum = null == gatherNum ? null : Long.valueOf(gatherNum) - aiReportDAO.countNum(EAiStatisticsType.CLEAN_DATA.getKey(), parentName);
        clean.put("num", cleanNum);
        clean.put("data", aiReportDAO.findAll(EAiStatisticsType.CLEAN_DATA.getKey(), parentName));

        Map<String, Object> model = Maps.newHashMap();
        model.put("num", null == cleanNum ? null : Math.round(cleanNum * 0.9));
        Double hour = LocalDateTimeUtils.timeDiff(LocalDateTimeUtils.localDateTime2String(LocalDateTimeUtils.dataToLocalDate(Long.valueOf(redisService.getStr(PRODUCT_DEPLOY_TIME)))),
                LocalDateTimeUtils.localDateTime2String(LocalDateTime.now())).intValue() * 0.03;
        model.put("hour", Math.round(hour));
        model.put("data", aiReportDAO.findAll(EAiStatisticsType.MODEL_DATA.getKey(), parentName));

        map.put("gatherData", gather);
        map.put("cleanData", clean);
        map.put("modelData", model);
        return map;
    }

    public Map<String, Object> findExceptPhosphorusStatistics(String parentName) {
        Map<String, Object> map = Maps.newHashMap();

        Map<String, Object> gather = Maps.newHashMap();
        String gatherNum = redisService.getStr(EXCEPT_PHOSPHORUS_ADD_GATHER_NUM);
        gather.put("num", gatherNum);
        gather.put("data", aiModelParamDAO.findPointCrrVal(parentName));

        Map<String, Object> clean = Maps.newHashMap();
        Long cleanNum = null == gatherNum ? null : Long.valueOf(gatherNum) - aiReportDAO.countNum(EAiStatisticsType.CLEAN_DATA.getKey(), parentName);
        clean.put("num", cleanNum);
        clean.put("data", aiReportDAO.findAll(EAiStatisticsType.CLEAN_DATA.getKey(), parentName));

        Map<String, Object> model = Maps.newHashMap();
        model.put("num", null == cleanNum ? null : Math.round(cleanNum * 0.9));
        Double hour = LocalDateTimeUtils.timeDiff(LocalDateTimeUtils.localDateTime2String(LocalDateTimeUtils.dataToLocalDate(Long.valueOf(redisService.getStr(PRODUCT_DEPLOY_TIME)))),
                LocalDateTimeUtils.localDateTime2String(LocalDateTime.now())).intValue() * 0.03;
        model.put("hour", Math.round(hour));
        model.put("data", aiReportDAO.findAll(EAiStatisticsType.MODEL_DATA.getKey(), parentName));

        map.put("gatherData", gather);
        map.put("cleanData", clean);
        map.put("modelData", model);
        return map;
    }

    public Map<String, Object> findExplosiveGasStatistics(String parentName) {
        Map<String, Object> map = Maps.newHashMap();

        Map<String, Object> gather = Maps.newHashMap();
        String gatherNum = redisService.getStr(EXPLOSIVE_GAS_ADD_GATHER_NUM);
        gather.put("num", gatherNum);
        gather.put("data", aiModelParamDAO.findPointCrrVal(parentName));

        Map<String, Object> clean = Maps.newHashMap();
        Long cleanNum = null == gatherNum ? null : Long.valueOf(gatherNum) - aiReportDAO.countNum(EAiStatisticsType.CLEAN_DATA.getKey(), parentName);
        clean.put("num", cleanNum);
        clean.put("data", aiReportDAO.findAll(EAiStatisticsType.CLEAN_DATA.getKey(), parentName));

        Map<String, Object> model = Maps.newHashMap();
        model.put("num", null == cleanNum ? null : Math.round(cleanNum * 0.9));
        Double hour = LocalDateTimeUtils.timeDiff(LocalDateTimeUtils.localDateTime2String(LocalDateTimeUtils.dataToLocalDate(Long.valueOf(redisService.getStr(PRODUCT_DEPLOY_TIME)))),
                LocalDateTimeUtils.localDateTime2String(LocalDateTime.now())).intValue() * 0.03;
        model.put("hour", Math.round(hour));
        model.put("data", aiReportDAO.findAll(EAiStatisticsType.MODEL_DATA.getKey(), parentName));

        map.put("gatherData", gather);
        map.put("cleanData", clean);
        map.put("modelData", model);
        return map;
    }

    public JSONObject findStatisticsNum(String parentName) {
        JSONObject map = new JSONObject();
        String redisKey = null;
        if ("智能碳源".equals(parentName)) redisKey = CARBON_SOURCE_ADD_GATHER_NUM;
        if ("智能除磷".equals(parentName)) redisKey = EXCEPT_PHOSPHORUS_ADD_GATHER_NUM;
        if ("智能曝气".equals(parentName)) redisKey = EXPLOSIVE_GAS_ADD_GATHER_NUM;
        String gatherNum = redisService.getStr(redisKey);
        map.put("gatherNum", gatherNum);
        Long cleanNum = null == gatherNum ? null : Long.valueOf(gatherNum) - aiReportDAO.countNum(EAiStatisticsType.CLEAN_DATA.getKey(), parentName);
        map.put("cleanNum", cleanNum);
        map.put("modelNum", null == cleanNum ? null : Math.round(cleanNum * 0.9));
        map.put("modelHour", Math.round(LocalDateTimeUtils.timeDiff(LocalDateTimeUtils.localDateTime2String(LocalDateTimeUtils.dataToLocalDate(Long.valueOf(redisService.getStr(PRODUCT_DEPLOY_TIME)))),
                LocalDateTimeUtils.localDateTime2String(LocalDateTime.now())).intValue() * 0.03));
        return map;
    }
}
